package com.cn.xly2023.service;

import com.cn.xly2023.mapper.GoodsMapper;
import com.cn.xly2023.pojo.Goods;
import com.cn.xly2023.pojo.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsService implements Serializable {
    @Resource
    private GoodsMapper goodsMapper;


    public Map<String,Object> selectById(Integer id){
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","查询成功!");
        map.put("data",goodsMapper.selectById(id));
        return map;
    }


    public Map<String,Object> selectAll(){
        List<Goods> goodsList = goodsMapper.selectAll();
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","登陆成功!");
        map.put("count",goodsList.size());
        map.put("data",goodsList);
        return map;
    }

    public Map<String,Object> selectByPage( Integer page, Integer limit,String goodsname,String price){
        Integer index = (page - 1) *limit;
        List<Goods> userList = goodsMapper.selectByPage(index,limit,goodsname,price);
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","登陆成功!");
        map.put("count",goodsMapper.selectCount(goodsname,price));
        map.put("data",userList);
        return map;
    }



    public Map<String,Object> insert(Goods goods){
        goods.setInsertTime(new Timestamp(System.currentTimeMillis()));//获取当前时间并且给insertTime赋值
        goodsMapper.insert(goods);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","插入成功!");
        map.put("data",goods);
        return map;
    }

    public Map<String,Object> deleteById(Integer id){
        goodsMapper.deleteById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","删除成功!");
        return map;
    }

//    public Map<String,Object> updateById(Integer id,String goodsname,String price,Integer num,String description){
//        goodsMapper.updateById(id, goodsname,price,num,description);

        public Map<String,Object> updateById(Goods goods){
        goodsMapper.updateById(goods);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","更新成功!");
        return map;
    }
}
